<script src="<?php print $url?>js/jquery.ui.core.js"></script>
<script src="<?php print $url?>js/jquery.ui.autocomplete.js"></script>
<script>
  function get_dot(asli, koma){
    asli = asli.toFixed(0);
    asli = asli.toString();
    var asli_temp = asli.split(".");
    var string = asli_temp[0];
    var string_hasil = "";
    
    var tanda = 0;
    var kl = string.length % 3;
    if(kl == 0)
      kl = 3;
    for(i = 0 ; i < string.length ; i++){
      if(tanda == 3 || i == kl){
        string_hasil = string_hasil + ".";
        tanda = 0;
        kl = 0;
      }
      string_hasil = string_hasil + string[i];
      tanda++;
    }
      if(!asli_temp[1])
        asli_temp[1] = '';
      
      return string_hasil;
  }
  function kill_dot(string){
    var string_hasil = "";
    for(i = 0 ; i < string.length ; i++){
      if(string[i] != "."){
        if(string[i] == ",")
          string_hasil = string_hasil + ".";
        else
          string_hasil = string_hasil + string[i];
      }
    }
    return string_hasil;
  }
  function auto_field(id){
    $.post('<?php print site_url("crm/quotation/auto-field")?>',{id:id},function(data){
      var view = $.parseJSON(data);
      $("#up_to").val(view.users);
      $("#telphone").val(view.telphone);
      $("#email").val(view.email);
//      alert(data);
    });
  }
  function auto_field_items(id, tr){
    $.post('<?php print site_url("crm/auto-field-products")?>',{id:id, tr:tr},function(data){
      var view = $.parseJSON(data);
      $("#price_"+tr).val(view.price);
      $("#discount_"+tr).val(view.discount);
      $("#qty_"+tr).val('1');
      total_price(tr);
    });
  }
  function total_price(tr){
    var qty = $("#qty_"+tr).val();
    var price = $("#price_"+tr).val();
    var discount = $("#discount_"+tr).val();
    var min_discount = qty * price;
    var total_product = min_discount - (min_discount * discount/100);
    $("#total_"+tr).text(get_dot(total_product, 0));
    total_seluruh();
  }
  function total_seluruh(){
    var max = $("#nomor_flag").val() * 1;
//    console.log(max);
    var sel = 0;
    for(r = 0 ; r <= max ; r++){
      var total_p = kill_dot($("#total_"+r).text()) * 1;
//      console.log(total_p);
      sel = sel + total_p;
    }
    $("#total_utama").text(get_dot(sel, 0));
  }
  function add_row_tambahan(table,url,act,param){
    var no = parseInt($('#'+param).val());
    $('#'+param).val(no+1);
    $.post(url,{no:$('#'+param).val(),action:act},function(data){
        $(data).clone(true).insertBefore("#"+table+" #"+act);
    });
  }
  function del_row_lokal(tr){
    $("#"+tr).remove();
//    total_seluruh();
  }
  $(document).ready( function () {
    total_seluruh();
  });
</script>
<div id="main-content">
  <div class="container-fluid">
    <div class="row-fluid">
			<div class="span12">
				<div class="nonboxy-widget">
					<div class="widget-head">
						<h5><i class="black-icons blocks_images"></i><?php print $title?></h5>
					</div>
					<div class="widget-content">
						<div class="widget-box">
              <?php print $this->form_eksternal->form_open("", 'class=" form-horizontal well" id="formoutlet"', 
                      array("id_detail" => $detail[0]->id_crm_quotation))?>
								<fieldset>
									<div class="control-group">
										<label class="control-label"><?php print lang("master_market_target")?></label>
										<div class="controls">
                      <?php print $this->form_eksternal->form_autocomplate("market_target", "id_crm_market_target", "market_target", "id_crm_market_target", 
                              site_url("crm/auto-target-market"), $detail[0]->target_market, $detail[0]->id_crm_market_target, "", 
                              "auto_field(ui.item.id);")?>
										</div>
									</div>
									<div class="control-group">
										<label class="control-label"><?php print lang("user")?></label>
										<div class="controls">
                      <?php print $this->form_eksternal->form_input('up_to', $detail[0]->up_to, "id='up_to'")?>
										</div>
									</div>
									<div class="control-group">
										<label class="control-label"><?php print lang("telphone")?></label>
										<div class="controls">
                      <?php print $this->form_eksternal->form_input('telphone', $detail[0]->telphone, "id='telphone'")?>
										</div>
									</div>
									<div class="control-group">
										<label class="control-label"><?php print lang("email")?></label>
										<div class="controls">
                      <?php print $this->form_eksternal->form_input('email', $detail[0]->email, "id='email'")?>
										</div>
									</div>
									<div class="control-group">
										<label class="control-label"><?php print lang("note")?></label>
										<div class="controls">
                      <?php print $this->form_eksternal->form_textarea('note', $detail[0]->info, "style='height: 60px'")?>
										</div>
									</div>
                  <div class="control-group">
                    <?php print $this->form_eksternal->form_addrow_tr(
                            array(lang("product_name"), lang("product_specification"), lang("qty"), lang("sell_price"), lang("product_discount"), 
                            lang("total_price")), $isi, site_url("crm/quotation/add-products"), "items", "id_items", "nomor_flag", $tr, "width='100%'", 
                            true, true, true)?>
                  </div>
                  <div class="control-group">
                    <?php print $this->form_eksternal->form_input('param', count($isi), 'id="nomor_flag" style="display: none;"')?>
                  </div>
								</fieldset>
								<div class="form-actions">
									<button class="btn btn-primary" type="submit"><?php print lang("save")?></button>
									<button class="btn" onclick="history.go(-1);return false;"><?php print lang("cancel")?></button>
								</div>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
  </div>
</div>